package code.google.webactioncontainer.test;

import code.google.webactioncontainer.ActionContext;
import code.google.webactioncontainer.ActionSupport;
import code.google.webactioncontainer.FileUploaded;
import code.google.webactioncontainer.PageContext;
import code.google.webactioncontainer.ParameterObject;
import code.google.webactioncontainer.SessionContext;
import code.google.webactioncontainer.annotation.ActionMethod;
import code.google.webactioncontainer.annotation.WebAction;
import code.google.webactioncontainer.dao.DAO;
import code.google.webactioncontainer.validate.FileValidation;
import code.google.webactioncontainer.validate.IntegerValidation;
import code.google.webactioncontainer.validate.StringValidation;
import code.google.webactioncontainer.validate.ValidateException;

import java.util.Random;

/**
 * @author <a href="mailto:jfox.young@gmail.com">Young Yang</a>
 */
@WebAction(name = "example")
public class ExampleAction extends ActionSupport {
    @DAO
    ExampleDAO exampleDAO;    

    @ActionMethod(name = "hello", successView = "example/hello.vhtml")
    public void helloMethod(ActionContext actionContext) throws Exception {
        PageContext pageContext = actionContext.getPageContext();
        pageContext.setAttribute("hello", "Hello, WebActionContainer!");
    }

    @ActionMethod(name = "formview",successView = "example/form.vhtml")
    public void formViewAction(ActionContext actionContext) throws Exception {
        
    }

    @ActionMethod(name = "form", successView = "example/form.vhtml", errorView = "example/form.vhtml", parameterClass = FormParameterObject.class, httpMethod = ActionMethod.HttpMethod.POST)
    public void formAction(ActionContext actionContext) throws Exception {
        FormParameterObject formParameterObject = (FormParameterObject)actionContext.getParameterObject();
    }

    public static class FormParameterObject extends ParameterObject {
        @StringValidation(nullable = false, minLength = 4, errorId="1")
        private String name;

        @StringValidation(nullable = false, minLength = 4, errorId = "2")
        private String password;

        @StringValidation(nullable = false, minLength = 4, errorId = "2.1")
        private String reenterPassword;

        @IntegerValidation(nullable = false, minValue = 1, maxValue = 120, errorId = "4")
        private int age;

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getPassword() {
            return password;
        }

        public void setPassword(String password) {
            this.password = password;
        }

        public String getReenterPassword() {
            return reenterPassword;
        }

        public void setReenterPassword(String reenterPassword) {
            this.reenterPassword = reenterPassword;
        }

        @Override
        public void validateAll() throws ValidateException {
            if(getReenterPassword() == null || !getReenterPassword().equals(getPassword())){
                throw new ValidateException("3", "reenterPassword", getReenterPassword(), "reenterPassword doesn't match with the password.");
            }
        }
    }

    // --------- upload example ----------//
    @ActionMethod(name = "upload", successView = "example/form.vhtml", errorView = "example/form.vhtml", parameterClass = UploadParameterObject.class, httpMethod = ActionMethod.HttpMethod.POST)
    public void uploadAction(ActionContext actionContext) throws Exception {
        PageContext pageContext = actionContext.getPageContext();
        UploadParameterObject uploadParameterObject = (UploadParameterObject)actionContext.getParameterObject();
        FileUploaded fileUploaded = uploadParameterObject.getUploadFile();
        pageContext.setAttribute("uploaded", true);
        pageContext.setAttribute("uploadfilename", fileUploaded.getFilename());
        pageContext.setAttribute("uploadfilecontent", new String(fileUploaded.getContent()));
    }


    public static class UploadParameterObject extends ParameterObject {

        @FileValidation(nullable = false, suffix = ".txt", errorId = "5")
        private FileUploaded uploadFile;

        public FileUploaded getUploadFile() {
            return uploadFile;
        }

        public void setUploadFile(FileUploaded uploadFile) {
            this.uploadFile = uploadFile;
        }
    }

    //-------- NUMBER GUESS -------//

    @ActionMethod(name="numberguess", successView = "example/numberguess.vhtml", parameterClass = NumberGuessParameterObject.class)
    public void numberGuessView(ActionContext actionContext) throws Exception {
        NumberGuessParameterObject invocation = (NumberGuessParameterObject)actionContext.getParameterObject();

        int count = 0;
        boolean success = false;
        String hint = "";

        SessionContext sessionContext = actionContext.getSessionContext();
        if (!sessionContext.containsAttribute("count")) { //start
            count = 0;
            int answer = Math.abs(new Random().nextInt() % 100) + 1;
            sessionContext.setAttribute("answer", answer);
            sessionContext.setAttribute("count", 0);
        }
        else {
            int answer = (Integer)sessionContext.getAttribute("answer");
            count = (Integer)sessionContext.getAttribute("count");
            int guessNO = invocation.getGuessNO();
            if (guessNO == answer) { // success
                success = true;
                sessionContext.removeAttribute("count");
                sessionContext.removeAttribute("answer");
            }
            else { // failed
                if (guessNO < answer) {
                    hint = "higher";
                }
                else {
                    hint = "lower";
                }
                count++;
                sessionContext.setAttribute("count", count);
            }
        }

        PageContext pageContext = actionContext.getPageContext();
        pageContext.setAttribute("success", success);
        pageContext.setAttribute("count", count);
        pageContext.setAttribute("hint", hint);

    }

    public static class NumberGuessParameterObject extends ParameterObject {
        private int guessNO = 0;

        public int getGuessNO() {
            return guessNO;
        }

        public void setGuessNO(int guessNO) {
            this.guessNO = guessNO;
        }
    }

    //------ checkbox example ----/
    @ActionMethod(name="checkbox", successView = "example/checkbox.vhtml", parameterClass = CheckboxParameterObject.class)
    public void doPostSubmit(ActionContext actionContext) throws Exception{
        CheckboxParameterObject invocation = (CheckboxParameterObject)actionContext.getParameterObject();
        String[] fruits = invocation.getFruit();
        PageContext pageContext = actionContext.getPageContext();
        pageContext.setAttribute("fruits", fruits);
    }

    public static class CheckboxParameterObject extends ParameterObject {
        String[] fruit = new String[0];

        public String[] getFruit() {
            return fruit;
        }

        public void setFruit(String[] fruit) {
            this.fruit = fruit;
        }
    }

}
